package day0402

import (
	"fmt"
	"math"
)

func maximalSquare(matrix [][]byte) int {
	m, n := len(matrix), len(matrix[0])
	dp := make([][]int, m)
	for i := 0; i < m; i++ {
		dp[i] = make([]int, n)
	}
	for i := 0; i < m; i++ {
		if matrix[i][0] == '1' {
			dp[i][0] = 1
		} else {
			dp[i][0] = 0
		}
	}
	for i := 0; i < n; i++ {
		if matrix[0][i] == '1' {
			dp[0][i] = 1
		} else {
			dp[0][i] = 0
		}
	}
	res := 0
	for i := 1; i < m; i++ {
		for j := 1; j < n; j++ {
			if matrix[i][j] == '0' {
				dp[i][j] = 0
			} else {
				if dp[i-1][j] >= dp[i-1][j-1] && dp[i][j-1] >= dp[i-1][j-1] {
					dp[i][j] = dp[i-1][j-1] + 1
				} else {
					dp[i][j] = int(matrix[i][j] - '0')
				}
			}
			if dp[i][j] > res {
				res = dp[i][j]
			}
		}
	}
	fmt.Println(dp)
	return int(math.Pow(float64(res), float64(2)))
}
